package com.sc.nft.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.google.common.collect.Lists;
import com.sc.nft.admin.request.*;
import com.sc.nft.admin.vo.SimpleTaskPanelVO;
import com.sc.nft.admin.vo.TaskDetailVO;
import com.sc.nft.admin.vo.TaskPageVO;
import com.sc.nft.admin.vo.TaskPanelVO;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.*;
import com.sc.nft.dto.TaskPrizeDTO;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.task.LinkDTO;
import com.sc.nft.enums.task.ParticipantsEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.TaskPanelService;
import com.sc.nft.service.TaskService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/task")
@Api(tags="后台任务管理模块")
public class TaskManageController {

    private final TaskPanelService taskPanelService;
    private final TaskAppointProductDao taskAppointProductDao;
    private final TaskPanelDao taskPanelDao;
    private final TaskService taskService;
    private final TaskPrizeDao taskPrizeDao;
    private final EquityPropsDao equityPropsDao;
    private final DigitalCollectionDao digitalCollectionDao;
    @PostMapping(value="/taskPanelList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "任务模板列表",notes="任务模板列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<List<TaskPanelVO>> taskPanelList(@Valid @RequestBody PageRequest request){
        return Result.successColl(taskPanelService.getTaskPanel(),TaskPanelVO.class);
    }


    @PostMapping(value="/updateTaskPanelStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改任务模板状态",notes="修改任务模板状态", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateTaskPanelStatus(@Valid @RequestBody UpdateTaskPanelStatusRequest request){
        TaskPanel taskPanel = taskPanelDao.getById(request.getId());
        Assert.notNull(taskPanel,()->new GlobalRunTimeException("任务模板不存在"));
        taskPanel.setStatus(request.getStatus());
        taskPanelDao.updateById(taskPanel);
        return Result.success();
    }

    @PostMapping(value="/updateTaskPanelBusinessStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改任务模板商家可用状态",notes="修改任务模板商家可用状态", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateTaskPanelBusinessStatus(@Valid @RequestBody UpdateTaskPanelStatusRequest request){
        TaskPanel taskPanel = taskPanelDao.getById(request.getId());
        Assert.notNull(taskPanel,()->new GlobalRunTimeException("任务模板不存在"));
        taskPanel.setBusinessUseStatus(request.getStatus());
        taskPanelDao.updateById(taskPanel);
        return Result.success();
    }

    @PostMapping(value="/addTask", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加任务",notes="添加任务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addTask(@Valid @RequestBody AddTaskRequest request){
        taskService.addTask(request.getTaskIcon(),request.getTaskPlan(), request.getTaskName(),request.getTaskPanelId(),request.getBuyCollectionId(),
                request.getInviteCount(),request.getPartakeCollectionId(),request.getParticipants(),request.getSuperiorLimit(),
                request.getTaskStartDate(),request.getTaskEndDate(),request.getTaskPrizeDTOS(),request.getSort(),request.getTotalPrice(),request.getCollectionRange(),request.getMallRange(),request.getLinkIds());
        return Result.success();
    }

    @PostMapping(value="/addMerchantTask", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加商户任务",notes="添加任务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addMerchantTask(@Valid @RequestBody AddMerchantTaskRequest request){
        taskService.addTask(request.getTaskIcon(),request.getTaskPlan(),request.getTaskName(),request.getTaskPanelId(),request.getBuyCollectionId(),
                request.getInviteCount(),request.getPartakeCollectionId(),request.getParticipants(),request.getSuperiorLimit(),
                request.getTaskStartDate(),request.getTaskEndDate(),request.getTaskPrizeDTOS(),request.getSort(),request.getTotalPrice(),request.getCollectionRange(),request.getMallRange(),request.getLinkIds());
        return Result.success();
    }

    @PostMapping(value="/editTask", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑任务",notes="编辑任务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result editTask(@Valid @RequestBody EditTaskRequest request){
        taskService.editTask(request.getId(),request.getTaskIcon(),request.getTaskPlan(),request.getTaskName(),request.getTaskPanelId(),request.getBuyCollectionId(),
                request.getInviteCount(),request.getPartakeCollectionId(),request.getParticipants(),request.getSuperiorLimit(),
                request.getTaskStartDate(),request.getTaskEndDate(),request.getTaskPrizeDTOS(),request.getSort(),request.getTotalPrice(),request.getCollectionRange(),request.getMallRange(),request.getLinkIds());
        return Result.success();
    }

    @PostMapping(value="/simpleTaskPanel", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "简单任务模板列表",notes="简单任务模板列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<SimpleTaskPanelVO>> simpleTaskPanel(){
        List<TaskPanel> taskPanels = taskPanelDao.taskPanelList();
        return Result.successColl(taskPanels,SimpleTaskPanelVO.class);
    }
    @PostMapping(value="/getTaskDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "任务详情",notes="任务详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<TaskDetailVO> getTaskDetail(@Valid @RequestBody IdRequest request){
        Task task = taskService.getById(request.getId());
        TaskDetailVO vo = BeanUtil.toBean(task, TaskDetailVO.class);
        DigitalCollection byId = digitalCollectionDao.getById(vo.getBuyCollectionId());
        if (BeanUtil.isNotEmpty(byId)) {
            vo.setBuyCollectionName(byId.getFirstTitle());
        }
        if (ParticipantsEnum.APPOINT.equals(vo.getParticipants())) {
            DigitalCollection appoint = digitalCollectionDao.getById(vo.getPartakeCollectionId());
            if (BeanUtil.isNotEmpty(appoint)){
                vo.setPartakeCollectionName(appoint.getFirstTitle());
            }
        }
        TaskPanel taskPanel = taskPanelDao.getById(vo.getTaskPanelId());
        vo.setConclude(taskPanel.getConclude());
        List<TaskPrize> taskPrize = taskPrizeDao.getTaskPrize(task.getId());
        List<LinkDTO> linkDTOS = Lists.newArrayList();
        List<TaskAppointProduct> appointProducts = taskAppointProductDao.getByTaskId(task.getId());
        for (TaskAppointProduct appointProduct : appointProducts) {
            linkDTOS.add(new LinkDTO(appointProduct.getLinkId(),appointProduct.getLinkName(),appointProduct.getLinkImage()));
        }
        vo.setLinkDTOList(linkDTOS);
        List<TaskPrizeDTO> taskPrizeDTOS = Lists.newArrayList();
        for (TaskPrize prize : taskPrize) {
            TaskPrizeDTO taskPrizeDTO = new TaskPrizeDTO();
            taskPrizeDTO.setType(prize.getType());
            taskPrizeDTO.setPropsId(prize.getEquityId());
            taskPrizeDTO.setTouristsPrize(prize.getTouristsPrize());
            taskPrizeDTO.setResidentPrize(prize.getResidentPrize());
            switch (taskPrizeDTO.getType()) {
                case INTEGRAL:
                    taskPrizeDTO.setPropsName("积分");
                    break;
                case PROPS:
                    EquityProps equityProps = equityPropsDao.getById(prize.getEquityId());
                    taskPrizeDTO.setPropsName(equityProps.getFirstTitle());
                    break;
            }
            taskPrizeDTO.setCityPrize(prize.getCityPrize());
            taskPrizeDTOS.add(taskPrizeDTO);
        }
        vo.setTaskPrizeDTOS(taskPrizeDTOS);
        return Result.success(vo);
    }


    @PostMapping(value="/delTask", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除任务",notes="删除任务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result delTask(@Valid @RequestBody IdRequest request){
        taskService.delTask(request.getId());
        return Result.success();
    }


    @PostMapping(value="/taskPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "任务列表",notes="任务列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<TaskPageVO> taskPage(@Valid @RequestBody TaskPageRequest request){
        return PageResult.success(taskService.taskPage(request.getName(),request.getStatus(),request.getDaoId(),request.getPageNo(),request.getPageSize()),TaskPageVO.class);
    }

    @PostMapping(value="/taskMerchantPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商户任务列表",notes="任务列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<TaskPageVO> taskMerchantPage(@Valid @RequestBody TaskPageRequest request){
        return PageResult.success(taskService.taskMerchantPage(request.getName(),request.getStatus(),request.getDaoId(),request.getPageNo(),request.getPageSize()),TaskPageVO.class);
    }

    @PostMapping(value="/auditMerchantTask", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "审核商户任务",notes="审核商户任务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result auditMerchantTask(@Valid @RequestBody AuditMerchantTaskRequest request){
        taskService.auditMerchantTask(request.getId(),request.getStatus(),request.getReason());
        return Result.success();
    }

    @PostMapping(value="/updateTaskStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "更新上下架状态",notes="更新上下架状态", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result updateTaskStatus(@Valid @RequestBody UpdateTaskPanelStatusRequest request){
        taskService.updateStatus(request.getId(), request.getStatus());
        return Result.success();
    }

    @PostMapping(value="/changeTaskOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "变更任务排序",notes="变更任务排序", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result changeTaskOrder(@Valid @RequestBody ChangeTaskOrderRequest request){
        taskService.changeTaskOrder(request.getId(),request.getSort());
        return Result.success();
    }
}
